// pages/list/list.js
const request = require("../../utils/request")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    category: [],
    id: 0,
    value: "",
    sum: 0,
    params: {
      _page: 1,
      _limit: 10,
      q: ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    wx.setNavigationBarTitle({
        title: '西游记',
      }),
      this.setData({
        id: options.cat
      })
    this.init()
    console.log(options) //简易数据
  },
  
  init() {
    request(`/categories/${this.data.id}/shops`, this.data.params)
      .then(res => {
        console.log(res)
        this.setData({
          category: [...this.data.category,...res.data],
          sum: res.header['x-total-count']
        })
      })
  },
  //点击input框回车
  confirm(e){
    console.log(e)
    this.setData({
      category:[],
      params:{
        q:e.detail.value
      }
    })
    this.init()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let _page = this.data.params._page
    if (this.data.sum>_page*this.data.params._limit) {
      _page++
      this.setData({
        category:[],
       params:{
         _page,
         _limit: 10,
         q: ""
       }
      })
      console.log(this.data.params)
      this.init()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let _page = this.data.params._page
    if (this.data.sum > _page * this.data.params._limit) {
      _page++
      this.setData({
        params: {
          _page,
          _limit: 10,
          q: ""
        }
      })
      console.log(this.data.params)
      this.init()
    }
  },
})